<?php
/**
 * VerifyCodeTrait.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/05
 * Time: 21:26
 */
namespace app\common;

use think\captcha\Captcha;

trait VerifyCodeTrait
{
    /**
     * @param $verify_code
     * @throws \Exception
     */
    protected function checkVerifyCodeBySession($verify_code)
    {
        // 实例化验证码类
        $captcha = new Captcha();
        if( !$captcha->check($verify_code) ){
            throw new \Exception("验证码错误");
        }
        return true;
    }

    /**
     * 数字验证码
     * @return \think\Response
     */
    public function getVerifyCodeNum(){
        // 实例化验证码类
        $captcha = new Captcha();
        // 设置验证码长度
        $captcha->length = 4;
        // 设置验证码字符为纯数字
        $captcha->codeSet = '0123456789';
        // 关闭验证码杂点
        $captcha->useNoise = false;
        return $captcha->entry();
    }
}